<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\User;
use App\Transformers\UserTransformer;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    /**
     * Instanceof user
     *
     * @var User
     */
    private $model;

    /**
     * Instanceof userTransformer
     *
     * @var UserTransformer
     */
    private $transformer;


    /**
     * userController constructor.
     * @param User $model
     * @param UserTransformer $transformer
     */
    public function __construct(User $model, UserTransformer $transformer)
    {
        $this->model = $model;
        $this->transformer = $transformer;
        $this->middleware('jwt.auth', ['except' => ['index', 'show']]);
        $this->middleware('jwt.refresh', ['except' => ['index', 'show']]);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function index(Request $request)
    {
        $users = $this->model->filter($request->all())->paginate();
        return $this->response->paginator($users, $this->transformer);
    }

    /**
     * @param $id
     * @return \Dingo\Api\Http\Response|mixed
     */
    public function show($id)
    {
        $user = $this->model->query()->find($id);
        if (!$user) {
            return $this->response->errorNotFound();
        }
        return $this->response->item($user, $this->transformer);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response|mixed
     * @throws \Throwable
     */
    public function store(Request $request)
    {
        $this->validateField($request, $this->createRules());
        $user = $this->model->query()->create($request->all());
        if (!$user) {
            return $this->response->errorInternal('Error occurred on creating user');
        }
        return $this->response->item($user, $this->transformer);
    }

    /**
     * @param Request $request
     * @param $id
     * @return \Dingo\Api\Http\Response|mixed
     * @throws \Throwable
     */
    public function update(Request $request, $id)
    {
        $user = $this->model->query()->find($id);
        if (!$user) {
            return $this->response->errorNotFound();
        }
        $this->validateField($request, $this->updateRules($user));
        $saved = $user->update($request->only($this->fields()));
        if (!$saved) {
            return $this->response->errorInternal('Error occurred on updating user');
        }
        return $this->response->item($user, $this->transformer);
    }

    /**
     * @param $id
     * @return \Dingo\Api\Http\Response|mixed
     * @throws \Exception
     */
    public function destroy($id)
    {
        $user = $this->model->query()->find($id);
        if (!$user) {
            return $this->response->errorNotFound();
        }
        $deleted = $user->delete();
        if (!$deleted) {
            return $this->response->errorInternal('Error occurred on updating user');
        }
        return $this->response->noContent();
    }

    /**
     * @return array
     */
    public function createRules(): array
    {
        return [

        ];
    }

    /**
     * @param User $model
     * @return array
     */
    public function updateRules($model): array
    {
        return [

        ];
    }

    /**
     * @return array
     */
    public function fields()
    {
        return [];
    }


}